var user = {
		init : function() {
			user.userGrid=$('#userGrid');
			user.memo = $('#memo');
			user.userAddDialog = $('#userAddDialog');
			user.code = $('#code');
			user.userAddFrom = $('#userAddFrom');
			user.userUpdateForm = $('#userUpdateForm');
			user.memoUpdate = $('#memoUpdate');
			user.userUpdateDialog = $('#userUpdateDialog');
			
			user.userGrid.datagrid({
			 	url:window.ctx + '/SystemServlet?command=userTable',
			 	fixed:true,
			    fitColumns: true,
			    striped: true,
			    rownumbers: true,
			    pagination: true,
			    pageSize: 5,
			    pageList: [5, 10, 15, 20],
			    pageNumber: 1,
			    sortName: 'code',
			    sortOrder: 'desc',
			    toolbar: '#userToolbar',
			    columns:[[
			        {field:'id', width:100, checkbox: true},
			        {field:'code', title:'账号', width:100, align:'center', sortable: true},
					{field:'name', title:'姓名', width:100, align:'center'},
					{field:'sex', title:'性别', width:100, align:'center'},
					{field:'phone', title:'电话', width:100, align:'center'},
					{field:'email', title:'邮箱', width:100, align:'center'},
					{field:'createTime', title:'创建时间', width:100, align:'center'},
					{field:'class__name', title:'班级', width:100, align:'center'},
					{field:'role__name', title:'角色名', width:100, align:'center'},
					{field:'state', title:'状态', width:100, align:'center'},
					{field:'memo', title:'备注', width:100, align:'center'}
			    ]],
				onDblClickRow: function(rowIndex, rowData) {
					user.toolbar.showUpdateDialog(rowData.id);
				},
				onBeforeLoad:function(param){
					var userToolbarName=$('#userToolbar-name').val();
					var userToolbarClassName=$('#userToolbar-className').val();
					/*var userToolbarTime=$('#userToolbar-time').value,*/
					
					param.userToolbarName=userToolbarName;
					param.userToolbarClassName=userToolbarClassName;
					/*param.userToolbarTime=userToolbarTime*/
				},
		});
		},
		toolbar: {
			showAddDialog: function() {
				//user.toolbar.showCombotree();
				//console.log(user.userAddDialog);
				user.userAddDialog.dialog('open');
				//$('#userAddDialog').dialog('open');
				user.code.focus();
				//坑，只能使用id选择器
				//user.age.numberspinner('setValue', 20);
			},
			hiddenAddDialog: function() {
				user.userAddDialog.dialog('close');
				user.toolbar.resetAddForm();
			},
			resetAddFrom: function() {
				user.userAddFrom.form('reset');
			},
			add: function() {
				if (user.userAddFrom.form('validate')) {
					$.messager.progress({
						text : '正在处理...'
					});
					$.post( window.ctx + '/SystemServlet?command=adduser', user.userAddFrom.serialize(), function(data) {
						if (data) {
							$.messager.show({
								title: '系统提示',
								msg: '新增成功！',
								timeout: 3000,
								showType: 'slide'
							});
							user.toolbar.resetAddFrom();
							user.userGrid.datagrid('reload');
						} else {
							$.messager.alert('系统提示', '新增失败！', 'error')
						}
						$.messager.progress('close');
						user.toolbar.hiddenAddDialog();
					});
				}
			},
			showUpdateDialog: function(id) {
				var getData = function(v) {
					$.messager.progress({
						text : '正在处理...'
					});
					$.post(window.ctx + '/SystemServlet?command=getuser', {id : v}, function(data) {
						if (data) {
							user.userUpdateForm.form('load', {
								id: data.id,
								phone: data.phone,
								email: data.email,
								class__name: data.class__name
							});
							$('#userUpdateDialog').dialog('open');
						} else {
							$.messager.alert('系统提示', '获取信息失败！', 'error')
						}
						$.messager.progress('close');
					}, 'json');
				}
				if (id) {
					getData(id);
				} else {
					var row = user.userGrid.datagrid('getSelections');
					if (row.length == 1) {
						getData(row[0].id);
					} else {
						$.messager.show({
							title: '系统提示',
							msg: '请选择一行数据进行修改！',
							timeout: 3000,
							showType: 'slide'
						});	
					}
				}
			},
			hiddenUpdateDialog: function() {
				$('#userUpdateDialog').dialog('close');
				user.toolbar.resetUpdateFrom();
			},
			resetUpdateFrom: function() {
				user.userUpdateForm.form('reset');
			},
			update: function() {
				if (user.userUpdateForm.form('validate')) {
					$.messager.progress({
						text : '正在处理...'
					});
					$.post(window.ctx +'/SystemServlet?command=updateUser',$("#userUpdateForm").serialize(), function(data) {
						if (data == 1) {
							$.messager.show({
								title: '系统提示',
								msg: '修改成功！',
								timeout: 3000,
								showType: 'slide'
							});
							user.toolbar.resetUpdateFrom();
							user.userGrid.datagrid('reload');
						} else {
							$.messager.alert('系统提示', '修改失败！', 'error');
						}
						$.messager.progress('close');
						user.toolbar.hiddenUpdateDialog();
					});
				}
			},


			updateState: function() {
					var row = user.userGrid.datagrid('getSelections');
					if (row.length == 1) {
						$.messager.progress({
							text : '正在处理...'
						});
						$.post(window.ctx +'/SystemServlet?command=updateStateUser', {id : row[0].id} , function(data) {
							if (data == 1) {
								$.messager.show({
									title: '系统提示',
									msg: '修改成功！',
									timeout: 3000,
									showType: 'slide'
								});
								user.userGrid.datagrid('reload');
							} else {
								$.messager.alert('系统提示', '修改失败！', 'error');
							}
							$.messager.progress('close');
						});
					} else {
						$.messager.show({
							title: '系统提示',
							msg: '请选择一行数据进行修改！',
							timeout: 3000,
							showType: 'slide'
						});	
					}
				
			},
			replacement: function() {
				var row = user.userGrid.datagrid('getSelections');
				if (row.length > 0) {
					var ids = [];
					$.each(row, function(i, v) {
						ids.push(v.id);
					});
					$.post(window.ctx +'/SystemServlet?command=replacement', {ids: ids.join(',')}, function(data) {
						if (data == 1) {
							$.messager.show({
								title: '系统提示',
								msg: '重置密码成功！',
								timeout: 3000,
								showType: 'slide'
							});
							user.userGrid.datagrid('reload');
						} else {
							$.messager.alert('系统提示', '重置密码失败！', 'error')
						}
						$.messager.progress('close');
					});
				} else {
					$.messager.show({
						title: '系统提示',
						msg: '请选择用户！',
						timeout: 3000,
						showType: 'slide'
					});	
				}
			},
			redo: function() {
				user.userGrid.datagrid('unselectAll');
			}
		}
}

$(function(){
	user.init();
});